<?php
require_once '../practice/includes/db.php';

$fileSize = 1; // 1 MB
//echo 1024*512;exit;
$file_name = date('ymdHis').'_'.$_FILES['avatar']['name'];

$source = $_FILES['avatar']['tmp_name'];
$dest = "uploads/$file_name";

pr($_FILES);

// copy($source, $dest);

if($_FILES['avatar']['error'] == 0) {

  if($_FILES['avatar']['size'] < 1024*1024*$fileSize){
    $is_moved = move_uploaded_file($source, $dest);
    if($is_moved){
      echo success('Success !!!');
    } else {
      echo error('Failure !!!');
    }
  } else {
    echo error("Uploaded File size should not exceed to $fileSize MB");
  }
}

/*
 *  Code to generate thumb
 */
require_once ('thumbnail/thumb.php');
$img -> max_x = 100;
$img -> max_y = 100;
$img -> GenerateThumbFile($dest, "uploads/wall/$file_name");

 $img->max_x        = 50;
 $img->max_y        = 50;
 $img->GenerateThumbFile($dest, "uploads/chats/$file_name");


$data['avatar'] = $file_name;
$data['title'] = $_POST['title'];
$data['added_date'] = date('Y-m-d H:i:s');
$data['status'] = 1;

$flag = insert('user_images', $data);

if($flag){
  //echo success("Image uploaded successfully... :) ");
  header('Location:index.php');exit;
}


